package com.szt.sy.util.string;

import org.apache.commons.lang3.StringUtils;

/**
 * 字符串相关方法
 * 
 * @author wwy
 */
public class StringUtil {
	
	/**
	 * 将以逗号分隔的字符串转换成字符串数组
	 *
	 * @param valStr
	 * @return String[]
	 */
	public static String[] strList(String valStr) {
		int i = 0;
		String tempStr = valStr;
		String[] returnStr = new String[valStr.length() + 1 - tempStr.replace(",", "").length()];
		valStr = valStr + ",";
		while (valStr.indexOf(',') > 0) {
			returnStr[i] = valStr.substring(0, valStr.indexOf(','));
			valStr = valStr.substring(valStr.indexOf(',') + 1, valStr.length());
			
			i++;
		}
		return returnStr;
	}
	
	/***
	 * 验证是否是手机号码
	 *
	 * @param cellphone
	 * @return
	 */
	public static boolean checkCellphone(String cellphone) {
		String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
		return cellphone.matches(regex);
	}
	
	/**
	 * 获取字符串编码
	 *
	 * @param str
	 * @return
	 */
	public static String getEncoding(String str) {
		String encode = "GB2312";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s = encode;
				return s;
			}
		} catch (Exception exception) {
		}
		encode = "ISO-8859-1";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s1 = encode;
				return s1;
			}
		} catch (Exception exception1) {
		}
		encode = "UTF-8";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s2 = encode;
				return s2;
			}
		} catch (Exception exception2) {
		}
		encode = "GBK";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s3 = encode;
				return s3;
			}
		} catch (Exception exception3) {
		}
		return "";
	}
	
	/***
	 * 如果参数一为空，则使用参数二
	 *
	 * @param str
	 * @param defaultStr
	 * @return
	 */
	public static String isEmptyThen(String str, String defaultStr) {
		if (StringUtils.isEmpty(str)) {
			return defaultStr;
		} else {
			return str;
		}
	}
	
	/***
	 * 如果str为空，则输出thenStr，否则输入elseStr
	 *
	 * @param str
	 * @param thenStr
	 * @param elseStr
	 * @return
	 */
	public static String ifEmptyThenElse(String str, String thenStr, String elseStr) {
		if (StringUtils.isEmpty(str)) {
			return thenStr;
		} else {
			return elseStr;
		}
	}
	
	/***
	 * 隐藏银行卡号
	 *
	 * @param bankCard
	 * @return
	 */
	public static String maskBankCard(String bankCard) {
		String result = "************";
		if (bankCard.length() < 4) {
			return result;
		}
		String lastFour = bankCard.substring(bankCard.length() - 4);
		result += lastFour;
		return result;
	}
	
	public static void main(String[] args) {
		String bankCard = "35055532131";
		System.out.println(maskBankCard(bankCard));
	}
}
